﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class AddRoleDialog : ChildWindow
    {
        public string strSelectedValue="";
        public Employees employee;

        public AddRoleDialog()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Create the selected role for the selected employee if it does not yet exist
            await Globals.db.GetRoleObjects("Title='"+cmbRole.SelectedValue.ToString()+"'");
            await Globals.db.GetEmployeeRoleObjects("EmployeeID=" + Globals.selectedEmployee.col_ID + " and RoleID=" + Globals.db.RoleRecords[0].col_ID);
            if (Globals.db.EmployeeRoleRecords.Count > 0)
            {
                MessageBox.Show("Role title already exists for this employee.", "Duplicate Employee Role", MessageBoxButton.OK);
                employee.strSelectedRole = cmbRole.SelectedValue.ToString();
                employee.loadRoles();
            }
            else
            {
                ServiceReferenceOpenRPOS.EmployeeRole employeeRole = new ServiceReferenceOpenRPOS.EmployeeRole();
                employeeRole.bFilterActive = true;
                employeeRole.bFilterEmployeeID = true;
                employeeRole.bFilterRate = true;
                employeeRole.bFilterRoleID = true;
                employeeRole.col_Active = true;
                employeeRole.col_EmployeeID = Globals.selectedEmployee.col_ID;
                employeeRole.col_RoleID = Globals.db.RoleRecords[0].col_ID;
                employeeRole.col_Rate = 0;
                await Globals.db.AddEmployeeRoleObject(employeeRole);
                employee.strSelectedRole = cmbRole.SelectedValue.ToString();
                employee.loadRoles();
                this.DialogResult = true;
            }
           
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbLastName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // Prompt user to create a new role
            NewRoleDialog nrd = new NewRoleDialog();
            nrd.addRoleDialog = this;
            nrd.Show();
        }

        public async Task<int> loadRoles()
        {
            this.IsEnabled = false;
            // Determine if there are any inactive roles
            await Globals.db.GetRoleObjects("Active=0 order by Title");
            btnRestore.IsEnabled = Globals.db.RoleRecords.Count > 0;
            // Re-load the roles
            await Globals.db.GetRoleObjects("Active=1 order by Title");
            cmbRole.Items.Clear();
            foreach (ServiceReferenceOpenRPOS.Role role in Globals.db.RoleRecords)
            {
                cmbRole.Items.Add(role.col_Title);
            }
            if (cmbRole.Items.Count > 0)
            {
                if (strSelectedValue != "")
                    cmbRole.SelectedValue = strSelectedValue;
                else
                    cmbRole.SelectedIndex = 0;
            }
            this.IsEnabled = true;
            return cmbRole.Items.Count;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            EditRoleDialog erd = new EditRoleDialog();
            erd.tbRoleTitle.Text = cmbRole.SelectedValue.ToString();
            erd.addRoleDialog = this;
            erd.Show();
        }

        private async void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
            // First, prompt the user to be sure that they want to remove the selected role
            if (MessageBox.Show("Are you sure that you want to remove the selected role?", "Confirm", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                // Remove selected role
                await Globals.db.GetRoleObjects("Title='" + cmbRole.SelectedValue.ToString() + "'");
                Globals.db.RoleRecords[0].bFilterActive = true;
                Globals.db.RoleRecords[0].col_Active = false;
                await Globals.db.UpdateRoleObject(Globals.db.RoleRecords[0]);
                strSelectedValue = "";
                await loadRoles();
            }
            this.IsEnabled = true;
        }

        private void btnRestore_Click(object sender, RoutedEventArgs e)
        {
            // Create a "Restore Role" dialog, initialize it, and show it
            RestoreRoleDialog rrd = new RestoreRoleDialog();
            rrd.addRoleDialog = this;
            rrd.loadTitles();
            rrd.Show();
            this.IsEnabled = false;
        }

    }
}

